//
//  TrustViewController.m
//  XRSquare
//
//  Created by UaenaMo on 2018/8/14.
//  Copyright © 2018年 UaenaMo. All rights reserved.
//

#import "TrustViewController.h"
#import <Qiniu/QiniuSDK.h>

@interface TrustViewController ()<UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
    UIScrollView * contentView;
    UITextField * nameTextField;
    UITextField * idTextField;
    UIImageView * idimg1;
    UIImageView * idimg2;
    UIImageView * idimg3;
    
    
    NSString * img1;
    NSString * img2;
    NSString * img3;
    
    int currentImage;
    int currentUpload;
}
@end

@implementation TrustViewController

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = YES;
}

-(void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    currentUpload = 1;
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.title = @"实名认证";
    CGRect rectStatus = [[UIApplication sharedApplication] statusBarFrame];
    CGRect rectNav = self.navigationController.navigationBar.frame;
    
    contentView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, rectStatus.size.height + rectNav.size.height, ScreenWidth, ScreenHeight)];
    [self.view addSubview:contentView];
    
    UILabel * statusLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 20)];
    statusLabel.textAlignment = NSTextAlignmentCenter;
    statusLabel.font = [UIFont systemFontOfSize:15];
    [contentView addSubview:statusLabel];
    
    UILabel * naLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, ScreenWidth, 20)];
    naLabel.font = [UIFont systemFontOfSize:15];
    naLabel.text = @"姓名";
    [contentView addSubview:naLabel];
    
    UITextField * nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 80, ScreenWidth, 20)];
    nameTextField.placeholder = @"请输入姓名";
    nameTextField.font = [UIFont systemFontOfSize:15];
    [contentView addSubview:nameTextField];
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectMake(0, 25, ScreenWidth - 20, 1)];
    line.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [nameTextField addSubview:line];
    
    UILabel * idLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 120, ScreenWidth, 20)];
    idLabel.font = [UIFont systemFontOfSize:15];
    idLabel.text = @"身份证号码";
    
    [contentView addSubview:idLabel];
    
    UITextField * idTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 150, ScreenWidth, 20)];
    idTextField.placeholder = @"请输入身份证号";
    idTextField.font = [UIFont systemFontOfSize:15];
    [contentView addSubview:idTextField];
    
    UIView * line1 = [[UIView alloc]initWithFrame:CGRectMake(0, 25, ScreenWidth - 20, 1)];
    line1.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [idTextField addSubview:line1];
    
    
    UIView * sep1 = [[UIView alloc]initWithFrame:CGRectMake(0, 200, ScreenWidth, 5)];
    sep1.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [contentView addSubview:sep1];
    
    UIButton * pic1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 215, ScreenWidth, 80)];
    [pic1 addTarget:self action:@selector(pic1Action) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:pic1];
   
    idimg1 = [[UIImageView alloc]initWithFrame:pic1.bounds];
    idimg1.contentMode = UIViewContentModeScaleAspectFit;
    idimg1.image = [UIImage imageNamed:@"WechatIMG1915"];
    [pic1 addSubview:idimg1];
    
    UIView * sep2 = [[UIView alloc]initWithFrame:CGRectMake(0, 300, ScreenWidth, 5)];
    sep2.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [contentView addSubview:sep2];
    
    UIButton * pic2 = [[UIButton alloc]initWithFrame:CGRectMake(0, 315, ScreenWidth, 80)];
    [pic2 addTarget:self action:@selector(pic2Action) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:pic2];
    
    idimg2 = [[UIImageView alloc]initWithFrame:pic1.bounds];
    idimg2.contentMode = UIViewContentModeScaleAspectFit;
    idimg2.image = [UIImage imageNamed:@"WechatIMG1916"];
    [pic2 addSubview:idimg2];
    
    UIView * sep3 = [[UIView alloc]initWithFrame:CGRectMake(0, 400, ScreenWidth, 5)];
    sep3.backgroundColor = [UIColor colorFromHexCode:@"f3f3f3"];
    [contentView addSubview:sep3];
    
    UIButton * pic3 = [[UIButton alloc]initWithFrame:CGRectMake(0, 415, ScreenWidth, 80)];
    [pic3 addTarget:self action:@selector(pic3Action) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:pic3];
    
    idimg3 = [[UIImageView alloc]initWithFrame:pic1.bounds];
    idimg3.contentMode = UIViewContentModeScaleAspectFit;
    idimg3.image = [UIImage imageNamed:@"WechatIMG1917"];
    [pic3 addSubview:idimg3];
    
    [NetRequestCenter shimingResult:^(BOOL success, NSDictionary *resultDic) {
        if (success)
        {
            if ([[[resultDic objectForKey:@"data"]objectForKey:@"CheckResult"]isEqualToString:@"1"])
            {
                // 审核通过
                statusLabel.text = @"实名认证已通过";
                naLabel.hidden = YES;
                nameTextField.hidden = YES;
                idLabel.hidden = YES;
                idTextField.hidden = YES;
                pic1.hidden = YES;
                pic2.hidden = YES;
                pic3.hidden = YES;
            }
            else if ([[[resultDic objectForKey:@"data"]objectForKey:@"CheckResult"]isEqualToString:@"2"])
            {
                // 审核但是不通过
                statusLabel.text = @"实名认证未通过";
                self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(postAction)];

            }
            else
            {
                // 尚未审核
                if ([[[resultDic objectForKey:@"data"]objectForKey:@"ShenFenZhengName"]isEqualToString:@""])
                {
                    statusLabel.text = @"尚未审核";

                    nameTextField.text = [[resultDic objectForKey:@"data"]objectForKey:@"ShenFenZhengName"];
                    idTextField.text = [[resultDic objectForKey:@"data"]objectForKey:@"ShenFenZhengID"];
                    
                    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(postAction)];
                }
                else
                {
                    statusLabel.text = @"实名认证审核中";
                    naLabel.hidden = YES;
                    nameTextField.hidden = YES;
                    idLabel.hidden = YES;
                    idTextField.hidden = YES;
                    pic1.hidden = YES;
                    pic2.hidden = YES;
                    pic3.hidden = YES;
                }
            }
        }
    }];
}

-(void)pic1Action
{
    currentImage = 1;
    [self selectImage];
}

-(void)pic2Action
{
    currentImage = 2;
    [self selectImage];
}

-(void)pic3Action
{
    currentImage = 3;
    [self selectImage];
}

-(void)selectImage
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction * timeOrder = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = YES;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }];
    UIAlertAction * collectionOrder = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIImagePickerController * imagePicker = [[UIImagePickerController alloc]init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePicker.allowsEditing = YES;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"Cancel Action");
    }];
    
    [alertController addAction:timeOrder];
    [alertController addAction:collectionOrder];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage * selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    if (currentImage == 1) {
        idimg1.image = selectedImage;
    }
    else if (currentImage == 2)
    {
        idimg2.image = selectedImage;
    }
    else
    {
        idimg3.image = selectedImage;
    }
    
    [self dismissViewControllerAnimated:picker completion:nil];
}

-(void)imageUpload
{
    if (currentUpload == 4)
    {
        [self postAction];
    }
    else
    {
        [NetRequestCenter uptokenwithbucket:@"1" result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                NSString * token = [resultDic objectForKey:@"uptoken"];
                UIImage * oriImage;
                if (currentUpload == 1)
                {
                    oriImage = idimg1.image;
                }
                else if (currentUpload == 2)
                {
                    oriImage = idimg2.image;
                }
                else
                {
                    oriImage = idimg3.image;
                }
                NSData * imageData = UIImageJPEGRepresentation(oriImage, 0.5);
                QNUploadOption * option=[[QNUploadOption alloc] initWithProgressHandler:^(NSString *key, float percent) {
                    NSLog(@"key=%@ ,percent=%f",key,percent);
                }];
                
                QNUploadManager * upload=[[QNUploadManager alloc] init];
                
                [upload putData:imageData key:[[token componentsSeparatedByString:@":"]firstObject] token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *res) {
                    NSLog(@"statusCode=%d",info.statusCode);
                    NSLog(@"%@",res);
                    
                    if(info.statusCode==200)
                    {
                        currentUpload++;
                        [self imageUpload];
                    }
                    else
                    {
                        
                    }
                    
                } option:option];
            }
        }];
    }
}

-(void)postAction
{
    if (currentUpload == 1)
    {
        [self imageUpload];
    }
    else
    {
        [NetRequestCenter commitshimingWithName:nameTextField.text num:idTextField.text idpichand:img3 idpicfront:img1 idpicback:img2 licensename:nameTextField.text licenseid:idTextField.text licensepic:@"" result:^(BOOL success, NSDictionary *resultDic) {
            if (success)
            {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
